package co.yixiang.yshop.module.product.service.storeproductstocklog;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.storeproductstocklog.vo.StoreProductStockLogCreateReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductstocklog.vo.StoreProductStockLogPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductstocklog.vo.StoreProductStockLogRespVO;
import co.yixiang.yshop.module.product.convert.storeproductstocklog.StoreProductStockLogConvert;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductstocklog.StoreProductStockLogDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductstocklog.StoreProductStockLogMapper;
import co.yixiang.yshop.module.system.api.user.AdminUserApi;
import co.yixiang.yshop.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 商品库存记录 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class StoreProductStockLogServiceImpl implements StoreProductStockLogService {

    @Resource
    private StoreProductStockLogMapper storeProductStockLogMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createStoreProductStockLog(StoreProductStockLogCreateReqVO createReqVO) {
        // 插入
        StoreProductStockLogDO storeProductStockLog = StoreProductStockLogConvert.INSTANCE.convert(createReqVO);
        storeProductStockLogMapper.insert(storeProductStockLog);
        // 返回
        return storeProductStockLog.getId();
    }




    @Override
    public PageResult<StoreProductStockLogRespVO> getStoreProductStockLogPage(StoreProductStockLogPageReqVO pageReqVO) {
        PageResult<StoreProductStockLogRespVO> stockLogRespVOPageResult = storeProductStockLogMapper.selectPage(pageReqVO);
        stockLogRespVOPageResult.getList().forEach(item -> {
            AdminUserRespDTO userRespDTO = adminUserApi.getUser(item.getCreator());
            item.setCreateUser(userRespDTO.getNickname());
        });
        return stockLogRespVOPageResult;
    }

}
